package br.edu.sistemaacademico.builder;



import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Curso;
import br.edu.sistemaacademico.pojo.CursoAluno;
import br.edu.sistemaacademico.pojo.Matricula;

public class CursoAlunoDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "nome", "anoRegulamentado" });
	
	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Curso c = (Curso) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getNome(), c.getAnoRegulamentado() });

		String condicao = "codigo = " + c.getId();
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_CURSOALUNO");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<Object> getAll(String condicao, String tabelasAdicionais, String ordenacao){
		List<Curso> list =  new LinkedList<Curso>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_CursoAluno a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		select.colunasToReturn = "a.*";
		select.orderBy = ordenacao;
		
		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				list.add((Curso) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Curso c = new Curso();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_CursoAluno a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		select.setCondicao(condicao);
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				c = (Curso) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Curso c = (Curso) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getNome(), c.getAnoRegulamentado() });

		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_CursoAluno");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);

		try {
			System.out.println(insert.getSQL());
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		DataFacade facade = null;
		facade = new DataFacade(rs.getString(COLUNAS.get(2)));
		Curso a = new Curso();
		a.setId(rs.getLong(COLUNAS.get(0)));
		a.setNome(rs.getString(COLUNAS.get(1)));
		a.setAnoRegulamentado(facade.getDate());

		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Curso c = (Curso) o;
		List<Object> valores = Arrays.asList(new Object[] { c.getId(),
				c.getNome(), c.getAnoRegulamentado() });

		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_CursoAluno");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			System.out.println(delete.getSQL());
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
